
gpl <- data.table::fread("一开二次重启/data/GSE7158/GPL570-55999.txt",skip = '#') %>% select(1,V11)

colnames(gpl) <- gpl[1, ] %>% as.character()
gpl <- gpl[-1,]
i1 <- which(gpl[['Gene Symbol']] %>% str_detect('///'))
i2 <- which(gpl[['Gene Symbol']] == '')
i3 <- which(gpl[['ID']] %>% str_detect('/'))

anno <- gpl[-c(i1,i2,i3),]

df <- read_delim("一开二次重启/data/GSE7158/GSE7158_series_matrix.txt")

anno_gene <- df %>%
    left_join(anno %>%
        select(ID, 2) %>%
        rename(ID_REF = 1,GENE_SYMBOL = 2))


anno_gene %<>%
    select(-1) %>%
    select(GENE_SYMBOL, everything())

anno_gene %<>%
    filter(GENE_SYMBOL != "") %>%
    filter(!is.na(GENE_SYMBOL)) %>% 
    filter(!str_detect(GENE_SYMBOL,'///'))%>% 
    filter(GENE_SYMBOL != "---") %>% 
    rename(gene = 1)

m <- as.data.frame(anno_gene)
m <- exp_martix_aggregate(input_exp = m, rownames_type = "gene", fun = "median")
m <- m[, !str_detect(colnames(m), "Detection")]




# 方法2：
data_exprs_TPM[, clin_tmp$sample] %>% rownames_to_column("gene") -> tpm

# g_map <- get_map()

tpm %<>% mutate(gene = substr(gene, 1, 15))
inner_join(g_map %>% select(1, gene_name) %>% rename(gene = 1) %>% filter(g_map$gene_type == "protein_coding"), tpm) -> tpm


tpm %>% select(-1) %>% rename(gene = 1) -> tpm

which(duplicated(tpm$gene)) -> i

tpm %>% filter(gene %in% tpm$gene[i]) -> dup_expr
tpm %>% filter(!gene %in% tpm$gene[i]) -> no_dup_expr

dup_expr %<>% group_by(gene) %>% summarise_all(.funs = list(median))

bind_rows(no_dup_expr, dup_expr) %>%
    as.data.frame(expr) %>%
    column_to_rownames("gene") -> expr


deal_dup_expr <- \(expr, gene_col = "gene", type = "median"){
    colnames(expr)[colnames(expr) == gene_col] <- "gene"

    expr %>%
        select(-1) %>%
        rename(gene = 1) -> expr

    which(duplicated(expr$gene)) -> i

    expr %>% filter(gene %in% expr$gene[i]) -> dup_expr
    expr %>% filter(!gene %in% expr$gene[i]) -> no_dup_expr

    dup_expr %<>% group_by(gene) %>% summarise_all(.funs = get0(type))

    bind_rows(no_dup_expr, dup_expr) %>%
        as.data.frame(expr) %>%
        column_to_rownames("gene") -> expr

    return(expr)
}

